package wangm.util;

import wangm.bean.Pagination;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * db组件
 *
 * @author zk
 * @date 2018-02-23 14:29
 */
@Component
public class DBOperatorComponent {

    @Autowired
    private QueryRunner queryRunner;

    /**
     * 根据id获取该实体
     *
     * @param id        id
     * @param tableName tableName
     * @param clazz     clazz
     * @param <T>       t
     * @return T
     * @throws SQLException sql error
     */
    public <T> T getById(String id, String tableName, Class<T> clazz) throws SQLException {
        String sql = "select * from " + tableName + " where id = ?";
        return getBeanList(clazz, sql, id).get(0);
    }

    /**
     * 获取实体列表
     *
     * @param tableName tableName
     * @param clazz     clazz
     * @param <T>       T
     * @return list<T></>
     * @throws SQLException sql error
     */
    public <T> List<T> getBeanList(String tableName, Class<T> clazz) throws SQLException {
        String sql = "select * from ?";
        return getBeanList(clazz, sql, tableName);
    }

    /**
     * 根据预设sql和params获取实体
     *
     * @param clazz  clazz
     * @param sql    sql
     * @param params params
     * @param <T>    t
     * @return list<T></>
     * @throws SQLException sql error
     */
    public <T> List<T> getBeanList(Class<T> clazz, String sql, Object... params) throws SQLException {
        return queryRunner.query(sql, new BeanListHandler<T>(clazz), params);
    }

    /**
     * 根据预设sql和params获取实体
     *
     * @param clazz  clazz
     * @param sql    sql
     * @param params params
     * @param <T>    t
     * @return list<T></>
     * @throws SQLException sql error
     */
    public <T> List<T> getBeanList(Class<T> clazz, String sql, Pagination<T> pagination, Object... params) throws SQLException {
        Integer count = getBeanList(clazz,sql,params).size();
        pagination.setTotal(count);

        Integer beginRecord = (pagination.getPageNum() - 1) * pagination.getPageSize();
        sql = sql + "limit ?,?";

        //组装参数
        List<Object> parameters = new ArrayList<>(Arrays.asList(params));
        parameters.add(beginRecord);
        parameters.add(pagination.getPageSize());
        return getBeanList(clazz,sql, parameters.toArray());
    }

    /**
     * 根据预设sql和params 获取Object[] 集合
     *
     * @param sql    sql
     * @param params params
     * @return object[]
     * @throws SQLException sql error
     */
    public Object[] getArrayList(String sql, Object... params) throws SQLException {
        return queryRunner.query(sql, new ArrayHandler(), params);
    }

    /**
     * 执行update 语句
     *
     * @param sql    sql
     * @param params params
     * @param <T>    t
     */
    public <T> void update(String sql, Object... params) throws SQLException {
        queryRunner.update(sql, params);
    }

}
